package com.xiaogao.sys.controller;

import com.xiaogao.common.annotation.SysLogOperateAnnotation;
import com.xiaogao.common.entity.PageData;
import com.xiaogao.common.entity.Result;
import com.xiaogao.security.annotation.HasPerm;
import com.xiaogao.sys.request.user.AddParam;
import com.xiaogao.sys.request.user.QueryParam;
import com.xiaogao.sys.request.user.UpdateParam;
import com.xiaogao.sys.response.SysUserResponse;
import com.xiaogao.sys.response.SysUserSelect;
import com.xiaogao.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/sys/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;


    @PostMapping("/list")
    public Result<PageData<SysUserResponse>> querySelective(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "30") Integer pageSize,
            @RequestBody QueryParam queryParam) {
        PageData<SysUserResponse> pageData = sysUserService.querySelective(pageNum, pageSize, queryParam);
        return Result.success(pageData);
    }

    @SysLogOperateAnnotation("添加用户")
    @HasPerm("sys:user:add")
    @PostMapping("/add")
    public Result<Object> add(@RequestBody @Validated AddParam addParam) {
        sysUserService.add(addParam);
        return Result.success();
    }

    @HasPerm("sys:user:update")
    @PutMapping("/update")
    public Result<Object> update(@RequestBody @Validated UpdateParam updateParam) {
        sysUserService.update(updateParam);
        return Result.success();
    }

    @HasPerm("sys:user:delete")
    @PostMapping("/delete")
    public Result<Object> delete(@RequestBody List<Long> ids) {
        sysUserService.delete(ids);
        return Result.success();
    }

    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<String> uploadAvatar(@RequestParam("file") MultipartFile multipartFile) {
        String fileUrl = sysUserService.uploadAvatar(multipartFile);
        return Result.success(fileUrl);
    }

    @GetMapping("/select")
    public Result<List<SysUserSelect>> select() {
        List<SysUserSelect> selects = sysUserService.select();
        return Result.success(selects);
    }
}
